package com.web.my12306.dao.impl;

import com.web.my12306.dao.IUserDao;
import com.web.my12306.pojo.User;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class UserDaoImpl extends BaseDao implements IUserDao{
    /**
     * 方艺
     * 2017.9.12
     */
    //用户注册
    @Override
    public void userAdd(User user) {
        String sql="insert into user(loginname,password,img,name,sex,prov,city,cardtype,cardid,tel,utype,beizhu,state) " +
                "values(?,?,?,?,?,?,?,?,?,?,?,?,?)";
        update(sql,user.getLoginname(),user.getPassword(),user.getImg(),user.getName(),user.getSex(),user.getProv(),user.getCity(),
                user.getCardtype(),user.getCardid(),user.getTel(),user.getUtype(),user.getBeizhu(),user.getState());
    }


    //用户登录 by金刚天使葫芦娃
    public User login(String loginname, String password) {
        String sql="select * from user where loginname=? and password=?";
        query(sql,loginname,password);
        User user = null;
        //查询结果集
        try {
            while (rs.next()){
                user = new User();
                user.setUid(rs.getInt("uid"));
                user.setLoginname(rs.getString("loginname"));
                user.setPassword(rs.getString("password"));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return user;
    }

    @Override
    //修改密码时根据用户名查找原密码 by金刚天使葫芦娃
    public User findByLoginname(String loginname) {
        String sql="select * from user where loginname=? ";
        query(sql,loginname);
        User user = null;
        //查询结果集
        try {
            while (rs.next()){
                user = new User();
                user.setLoginname(rs.getString("loginname"));
                user.setPassword(rs.getString("password"));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return user;
    }

    @Override
    //修改密码 by金刚天使葫芦娃
    public void editPassword(String loginname,String password) {
        String sql="update user set password=?  where loginname=? ";
        update(sql,password,loginname);
    }
    @Override
    public List<User> queryALl()
    {
        List<User> users=new ArrayList<>();
        query("select * from user");
        try
        {
            while (rs.next())
            {
                User user=new User();
                user.setUid(rs.getInt("uid"));
                user.setLoginname(rs.getString("loginname"));
                user.setImg(rs.getString("img"));
                user.setName(rs.getString("name"));
                user.setSex(rs.getString("sex"));
//                user.setProv(rs.getString("prov"));
                user.setCity(rs.getString("city"));
                user.setCardtype(rs.getString("cardtype"));
                user.setCardid(rs.getString("cardid"));
                user.setTel(rs.getString("tel"));
                user.setUtype(rs.getString("utype"));
                user.setBeizhu(rs.getString("beizhu"));
                user.setState(rs.getInt("state"));
                users.add(user);
            }
        } catch (SQLException e)
        {
            closeAll();
        } finally
        {
        } return users;
    }
}
